﻿using Cinch;
using FR.SAFTAnalyser.Model;
using MEFedMVVM.Common;
using System;
using System.Text;
using System.Linq;
using FR.SAFTAnalyser.ViewModels.Pane;

namespace FR.SAFTAnalyser.ViewModels.Tools
{
    public class ErrorMessagesViewModel : ToolViewModel, ITool
    {
        public ErrorMessagesViewModel()
            : base("Erros")
        {
            if (!Designer.IsInDesignMode)
            {
                Mediator.Instance.Register(this);

                DoOpenViewCommand = new SimpleCommand<object, object>(CanOpenView, OpenView);
            }
        }

        public string NumErros { get; set; }
        private void InitNumErrors()
        {
            if (errorMessages == null || errorMessages.Length == 0)
                NumErros = "Não existem erros.";
            else
                NumErros = string.Format("Existem {0} erros.", errorMessages.Length);
            NotifyPropertyChanged(ObservableHelper.CreateArgs<ErrorMessagesViewModel>(x => x.NumErros));
        }

        Error[] errorMessages;
        public Error[] ErrorMessages
        {
            get { return errorMessages; }
            set
            {
                errorMessages = value;
                InitNumErrors();
                NotifyPropertyChanged(ObservableHelper.CreateArgs<ErrorMessagesViewModel>(x => x.ErrorMessages));
            }
        }

        Error selectedError;
        public Error SelectedError
        {
            get { return selectedError; }
            set
            {
                selectedError = value;
                NotifyPropertyChanged(ObservableHelper.CreateArgs<ErrorMessagesViewModel>(x => x.SelectedError));
            }
        }

        Error[] errorBackup;
        string filtro;
        public string Filtro
        {
            get { return filtro; }
            set
            {
                filtro = value;
                if (string.IsNullOrEmpty(filtro))
                    ErrorMessages = errorBackup;
                else
                {
                    ErrorMessages = (from e in ErrorMessages
                                     where e.Description.IndexOf(filtro, StringComparison.OrdinalIgnoreCase) >= 0
                                     select e).ToArray();
                }

                NotifyPropertyChanged(ObservableHelper.CreateArgs<ErrorMessagesViewModel>(x => x.Filtro));
            }
        }

        [MediatorMessageSink(MessageType.SAFT_HASH_RESULTS)]
        private void LoadHashValidationResults(Error[] error)
        {
            ErrorMessages = error;
            errorBackup = error;
        }
        [MediatorMessageSink(MessageType.ERROR_FOUND)]
        private void LoadErrorResults(Error[] error)
        {
            ErrorMessages = error;
            errorBackup = error;
        }

        [MediatorMessageSink(MessageType.SAFT_SCHEMA_RESULTS)]
        private void LoadSchemaValidationResults(Error[] error)
        {
            ErrorMessages = error;
            errorBackup = error;
        }

        public SimpleCommand<object, object> DoOpenViewCommand { get; private set; }
        private void OpenView(object args)
        {
            if (selectedError != null)
            {
                if (selectedError.TypeofError == typeof(SourceDocumentsSalesInvoicesInvoice))
                    Mediator.Instance.NotifyColleaguesAsync<string>(MessageType.FOCUS_INVOICES, selectedError.UID);
                if (selectedError.TypeofError == typeof(SourceDocumentsSalesInvoicesInvoiceLine))
                    Mediator.Instance.NotifyColleaguesAsync<string>(MessageType.FOCUS_INVOICES_LINE, string.Format("{0};{1}", selectedError.UID, selectedError.SupUID));
                else if (selectedError.TypeofError == typeof(Header))
                    Mediator.Instance.NotifyColleaguesAsync<string>(MessageType.FOCUS_HEADER, selectedError.UID);
                else if (selectedError.TypeofError == typeof(Customer))
                    Mediator.Instance.NotifyColleaguesAsync<string>(MessageType.FOCUS_CUSTOMERS, selectedError.UID);
                else if (selectedError.TypeofError == typeof(Product))
                    Mediator.Instance.NotifyColleaguesAsync<string>(MessageType.FOCUS_PRODUCTS, selectedError.UID);
                else if (selectedError.TypeofError == typeof(Supplier))
                    Mediator.Instance.NotifyColleaguesAsync<string>(MessageType.FOCUS_SUPPLIERS, selectedError.UID);
                else if (selectedError.TypeofError == typeof(Tax))
                    Mediator.Instance.NotifyColleaguesAsync<string>(MessageType.FOCUS_TAXS, selectedError.UID);
            }
        }

        private bool CanOpenView(object args)
        {
            return true;
        }

        PaneLocation preferredLocation = new PaneLocation { Location = PaneLocation.BOTTOM };
        public PaneLocation PreferredLocation
        {
            get { return preferredLocation; }
            set { preferredLocation = value; }
        }

        bool isAutoHidden = true;
        public bool IsAutoHidden
        {
            get { return isAutoHidden; }
            set { isAutoHidden = value; }
        }
    }
}
